package com.glxy.wxtakeaway.mapper;

import com.glxy.wxtakeaway.vo.Result;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

//柜子接口
public interface CabinetMapper {

//    1.送餐员打开柜子
    @Update({
            "UPDATE ${cabinetName}",
            "SET isOpen = 'open', phoneNumber = #{phoneNumber}",
            "WHERE id = (",
            "SELECT id FROM (",
            "SELECT id",
            "FROM ${cabinetName}",
            "WHERE (phoneNumber = '' OR phoneNumber IS NULL) AND id BETWEEN 1 AND 10",
            "ORDER BY id",
            "LIMIT 1",
            ") AS temp_table",
            ")"
    })
    boolean openCabinetForDeliveryman(@Param("cabinetName") String cabinetName,@Param("phoneNumber") String phoneNumber);

//    2.送餐员关闭柜子
    @Update({
            "UPDATE ${cabinetName}",
            "SET isOpen = 'close'",
            "WHERE id = (",
            "SELECT id FROM (",
            "SELECT id",
            "FROM ${cabinetName}",
            "WHERE phoneNumber = #{phoneNumber}",
            "ORDER BY id DESC",
            "LIMIT 1",
            ") AS temp",
            ")"
    })
    boolean closeCabinetForDeliveryman(@Param("cabinetName") String cabinetName,@Param("phoneNumber") String phoneNumber);

//    3.1取餐员查询是否有多条数据
    @Select("SELECT id FROM ${cabinetName} WHERE RIGHT(phoneNumber,4)=#{phoneNumber} AND isOpen = 'close'")
    List<Result> selectByPhoneNumber(@Param("cabinetName") String cabinetName, @Param("phoneNumber") String phoneNumber);

//    3.2取餐员打开对应柜子
    @Update("UPDATE ${cabinetName} SET isOpen = 'open' WHERE id = #{cabinetId}")
    boolean openCabinetForPickup(@Param("cabinetName") String cabinetName,@Param("cabinetId") Integer cabinetId);

//    4.取餐员关闭柜子
    @Update("UPDATE ${cabinetName} SET isOpen = 'close',phoneNumber='' WHERE id = #{cabinetId} AND RIGHT(phoneNumber, 4) = #{phoneNumber}")
    boolean closeCabinetForPickup(@Param("cabinetName") String cabinetName,@Param("cabinetId") Integer cabinetId,@Param("phoneNumber") String phoneNumber);

}
